<template>
  <div class="app-container">
    <div class="search-container">
      <!-- 查询 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline">

        <el-form-item label="断奶日期">
          <el-date-picker v-model="formInline.strartDate" type="date" placeholder="开始日期" clearable
            style="width: 150px;" />-
          <el-date-picker v-model="formInline.endDate" type="date" placeholder="结束日期" clearable style="width: 150px;" />
        </el-form-item>

        <el-form-item label="羔羊耳号">
          <el-input v-model="formInline.earTag" placeholder="请输入羔羊耳号" clearable />
        </el-form-item>

        <el-form-item label="断奶日龄">
          <el-input v-model="formInline.startMonthold" placeholder="起始日龄" clearable style="width: 150px;" />-
          <el-input v-model="formInline.endMonthold" placeholder="结束日龄" clearable style="width: 150px;" />
        </el-form-item>

        <el-form-item label="断奶重(公斤)">
          <el-input v-model="formInline.startWeaningWeight" placeholder="起始断奶重" clearable style="width: 150px;" />-
          <el-input v-model="formInline.endWeaningWeight" placeholder="结束断奶重" clearable style="width: 150px;" />
        </el-form-item>

        <el-form-item label="哺乳种母耳号">
          <el-input v-model="formInline.mammalianNumber" placeholder="请输入哺乳种母耳号" clearable />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="getLambList()">查询</el-button>
          <el-button type="danger" @click="pDel()">批量删除</el-button>
          <el-button type="success" @click="add()">添加</el-button>
        </el-form-item>
      </el-form>
      <!-- 数据 -->
      <el-table :data="LambList" border style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="lambWeaningId" label="序号" />
        <el-table-column prop="earTag" label="羔羊耳号" />
        <el-table-column prop="weaningAge" label="断奶日龄" />
        <el-table-column prop="weaningWeight" label="断奶重(公斤)" />
        <el-table-column prop="mammalianNumber" label="哺乳种母耳号" />
        <el-table-column prop="parity" label="胎次" />
        <el-table-column prop="currentBuildingName" label="断奶栋舍" />
        <el-table-column prop="currentFieldName" label="断奶栏位" />
        <el-table-column prop="toBuildingManageIdName" label="转入栋舍" />
        <el-table-column prop="toColumnManagementIdName" label="转入栏位" />
        <el-table-column prop="weaningDate" label="断奶日期" width="180px" />
        <el-table-column prop="createName" label="创建人" />
        <el-table-column prop="createTime" label="创建时间" width="180px" />
        <el-table-column fixed="right" label="操作" width="200px">
          <template v-slot="scope">
            <el-button link type="danger" @click="dels(scope.row.earTag)">删除</el-button>
            <el-button link type="primary" @click="edit(scope.row.earTag)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination v-model:current-page="formInline.pageIndex" v-model:page-size="formInline.pageSize"
        :page-sizes="[10, 15, 20, 25]" :background="true" layout="total, sizes, prev, pager, next, jumper"
        :total="pages.totalCount" @size-change="handleSizeChange" @current-change="handleCurrentChange" />

    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import LambWean from '@/api/Ablactationment/index'
import { lambweanQuery } from '@/api/Ablactationment/model'
import router from '@/router'
//参数
const formInline = reactive<lambweanQuery>({
  "strartDate": "",
  "endDate": "",
  "earTag": "",
  "startMonthold": null,
  "endMonthold": null,
  "startWeaningWeight": null,
  "endWeaningWeight": null,
  "mammalianNumber": "",
  "pageIndex": 1,
  "pageSize": 15
})
const pages = reactive({
  totalCount: 0,
  pageCount: 0
})
//分页
const handleSizeChange = (val: number) => {
  formInline.pageSize = val
  getLambList()
}
const handleCurrentChange = (val: number) => {
  formInline.pageIndex = val
  getLambList()
}
//数据
const LambList = ref([])
const getLambList = () => {
  LambWean.getLambWeanList(formInline).then(res => {
    LambList.value = res.data.data
    pages.totalCount = res.data.totalCount
    pages.pageCount = res.data.pageCount

  })
}
onMounted(() => {
  getLambList()
})

//删除
const dels = (eartagss: any) => {
  const eartags = [eartagss]

  ElMessageBox.confirm(
    '确认要删除吗?',
    '警告',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    LambWean.deltetes(eartags).then((res: any) => {
      if (res.code > 0) {
        ElMessage.success('删除成功');
        getLambList();
      }
    });
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '删除已取消',
    });
  });
};
//批量删除
const multipleSelection = ref([])
//批量删除
const handleSelectionChange = (val: any) => {
  val.forEach(element => {
    multipleSelection.value.push(element.lambWeaningId)
  });
}
//批量删除
const pDel = () => {
  const ids = reactive({
    ids: multipleSelection.value
  })
  ElMessageBox.confirm(
    '确认要删除吗?',
    '警告',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    LambWean.deltetes(ids).then((res: any) => {
      if (res.code > 0) {
        ElMessage.success('删除成功');
        getLambList();
      }
    });
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '删除已取消',
    });
  });
};
//添加
const add = () => {
  router.push('/AddAblactation')
}
//修改
const edit = (eartag: any) => {
  router.push({
    path: '/updIndex',
    query: {
      eartag: eartag
    }
  })
}
</script>